<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Action | Vuex</title>
    <meta name="description" content="Vue.js 的中心化状态管理方案">
    <link rel="icon" href="logo.png">
  <link rel="apple-touch-icon" href="https://vuex.vuejs.org/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="https://vuex.vuejs.org/icons/safari-pinned-tab.svg" color="#3eaf7c">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    
    <link rel="preload" href="https://vuex.vuejs.org/assets/css/0.styles.04c31208.css" as="style"><link rel="preload" href="https://vuex.vuejs.org/assets/js/app.92019be7.js" as="script"><link rel="preload" href="https://vuex.vuejs.org/assets/js/100.ff89a77e.js" as="script"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/10.9ff0229a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/101.75e31694.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/102.9eb5b6b9.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/103.84f39ee4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/104.1d8f5eda.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/105.22d02a0b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/106.6c7f9f4f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/107.df69838a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/108.57710a8f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/109.0b9350df.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/11.22ec5291.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/110.da276e13.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/111.d271062b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/112.fff28bd3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/12.4bb08316.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/13.eb7734ad.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/14.189de3ec.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/15.96301577.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/16.d35e6c41.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/17.6932f877.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/18.14d47735.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/19.f57a3619.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/20.4b3b43ce.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/21.fdbf3e9a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/22.38a06d07.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/23.20e52ca0.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/24.38c1ec6f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/25.d0749285.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/26.fa0199ca.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/27.104f01d5.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/28.0502b1eb.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/29.a3deb738.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/3.c2431962.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/30.5f61f97c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/31.c13e234c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/32.3346e47e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/33.9552f2b4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/34.22c87151.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/35.e274dae2.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/36.1f72edfe.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/37.3c8f5a6b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/38.18505477.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/39.fa1ab3f0.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/4.6114c173.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/40.82b0bf0f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/41.83249a92.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/42.dedbcb2b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/43.1626a294.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/44.46f3ccfa.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/45.5e63c438.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/46.4837dfdd.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/47.507f6518.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/48.56ef6c75.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/49.14767935.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/5.fea3e8c1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/50.a2cf5518.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/51.9a6f14ab.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/52.21aa240c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/53.a7ca4e09.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/54.18643ae3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/55.48be8fde.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/56.8123f4e5.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/57.3f666e3d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/58.a7e61993.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/59.8be4dcd4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/6.e165131f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/60.b6d2d182.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/61.eca98d13.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/62.5ee1323a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/63.c36ff790.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/64.b063e22d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/65.d5a56cb7.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/66.32060737.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/67.7b1f4ad9.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/68.61c1fb5a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/69.9eecf973.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/7.6a20bd14.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/70.5012a640.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/71.76c479ea.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/72.25f5be51.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/73.d31aea65.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/74.98a6bff3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/75.fa397fe2.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/76.8b008b0b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/77.280e8204.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/78.ba855657.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/79.58af331f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/8.259b04b8.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/80.a0635feb.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/81.762ec5d1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/82.0d4604b1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/83.9d064f79.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/84.72918f87.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/85.0d9821f3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/86.1631e292.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/87.6c6ced24.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/88.c8dfc6b4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/89.3e00d819.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/9.e8b16084.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/90.cd40ed9e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/91.71809249.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/92.aba0a727.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/93.c9b387fd.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/94.5be4c560.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/95.a52fb380.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/96.74307a3e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/97.0738b41c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/98.44547c3f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/99.9a86824d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/vendors~docsearch.0cca2532.js">
    
  
<link rel="stylesheet" type="text/css" href="actions.css" media="all">
</head>
<body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a indepth="true" href="index.html" class="home-link router-link-active"><!----> <span class="site-name">Vuex</span></a> <div class="links" style="max-width:nullpx;"><form action="https://vuex.vuejs.org/zh/guide/actions.html" id="search-form" class="algolia-search-wrapper search-box"><input value="" id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"><div class="nav-item"><a indepth="true" href="default.html" class="nav-link router-link-active">指南</a></div><div class="nav-item"><a indepth="true" href="default_001.html" class="nav-link">API 参考</a></div><div class="nav-item"><a href="https://github.com/vuejs/vuex/releases" target="_top" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display: none;"><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/guide/actions.html" class="nav-link">English</a></li><li class="dropdown-item"><!----> <a indepth="true" href="actions.html" class="nav-link router-link-exact-active router-link-active">简体中文</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ja/guide/actions.html" class="nav-link">日本語</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ru/guide/actions.html" class="nav-link">Русский</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/kr/guide/actions.html" class="nav-link">한국어</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ptbr/guide/actions.html" class="nav-link">Português</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/fr/guide/actions.html" class="nav-link">Français</a></li></ul></div></div> <a href="https://github.com/vuejs/vuex" target="_top" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a indepth="true" href="default.html" class="nav-link router-link-active">指南</a></div><div class="nav-item"><a indepth="true" href="default_001.html" class="nav-link">API 参考</a></div><div class="nav-item"><a href="https://github.com/vuejs/vuex/releases" target="_top" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display: none;"><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/guide/actions.html" class="nav-link">English</a></li><li class="dropdown-item"><!----> <a indepth="true" href="actions.html" class="nav-link router-link-exact-active router-link-active">简体中文</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ja/guide/actions.html" class="nav-link">日本語</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ru/guide/actions.html" class="nav-link">Русский</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/kr/guide/actions.html" class="nav-link">한국어</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ptbr/guide/actions.html" class="nav-link">Português</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/fr/guide/actions.html" class="nav-link">Français</a></li></ul></div></div> <a href="https://github.com/vuejs/vuex" target="_top" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><a indepth="true" href="installation.html" class="sidebar-link">安装</a></li><li><a indepth="true" href="index.html" class="sidebar-link">Vuex 是什么？</a></li><li><a indepth="true" href="default.html" class="sidebar-link">开始</a></li><li><div class="sidebar-group"><p class="sidebar-heading open">核心概念 <!----></p> <ul class="sidebar-group-items"><li><a indepth="true" href="state.html" class="sidebar-link">State</a></li><li><a indepth="true" href="getters.html" class="sidebar-link">Getter</a></li><li><a indepth="true" href="mutations.html" class="sidebar-link">Mutation</a></li><li><a indepth="true" href="actions.html" class="active sidebar-link">Action</a><ul class="sidebar-sub-headers"></ul></li><li><a indepth="true" href="modules.html" class="sidebar-link">Module</a></li></ul></div></li><li><a indepth="true" href="structure.html" class="sidebar-link">项目结构</a></li><li><a indepth="true" href="plugins.html" class="sidebar-link">插件</a></li><li><a indepth="true" href="strict.html" class="sidebar-link">严格模式</a></li><li><a indepth="true" href="forms.html" class="sidebar-link">表单处理</a></li><li><a indepth="true" href="testing.html" class="sidebar-link">测试</a></li><li><a indepth="true" href="hot-reload.html" class="sidebar-link">热重载</a></li></ul> </div> <div class="page"> <div class="content"><h1 id="action"><a href="#action" aria-hidden="true" class="header-anchor">#</a> Action</h1> <div class="scrimba"><a href="https://scrimba.com/p/pnyzgAP/c6ggR3cG" target="_top" rel="noopener noreferrer">在 scrimba 上尝试这节课</a></div> <p>Action 类似于 mutation，不同在于：</p> <ul><li>Action 提交的是 mutation，而不是直接变更状态。</li> <li>Action 可以包含任意异步操作。</li></ul> <p>让我们来注册一个简单的 action：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  state<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    count<span class="token punctuation">:</span> <span class="token number">0</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  mutations<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">increment</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      state<span class="token punctuation">.</span>count<span class="token operator">++</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">increment</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      context<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'increment'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象，因此你可以调用 <code>context.commit</code> 提交一个 mutation，或者通过 <code>context.state</code> 和 <code>context.getters</code> 来获取 state 和 getters。当我们在之后介绍到 <a indepth="true" href="modules.html">Modules</a> 时，你就知道 context 对象为什么不是 store 实例本身了。</p> <p>实践中，我们会经常用到 ES2015 的 <a href="https://github.com/lukehoban/es6features#destructuring" target="_top" rel="noopener noreferrer">参数解构<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 来简化代码（特别是我们需要调用 <code>commit</code> 很多次的时候）：</p> <div class="language-js extra-class"><pre class="language-js"><code>actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token function">increment</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'increment'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="分发-action"><a href="#分发-action" aria-hidden="true" class="header-anchor">#</a> 分发 Action</h3> <p>Action 通过 <code>store.dispatch</code> 方法触发：</p> <div class="language-js extra-class"><pre class="language-js"><code>store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'increment'</span><span class="token punctuation">)</span>
</code></pre></div><p>乍一眼看上去感觉多此一举，我们直接分发 mutation 岂不更方便？实际上并非如此，还记得 <strong>mutation 必须同步执行</strong>这个限制么？Action 就不受约束！我们可以在 action 内部执行<strong>异步</strong>操作：</p> <div class="language-js extra-class"><pre class="language-js"><code>actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token function">incrementAsync</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'increment'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Actions 支持同样的载荷方式和对象方式进行分发：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 以载荷形式分发</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'incrementAsync'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  amount<span class="token punctuation">:</span> <span class="token number">10</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 以对象形式分发</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> <span class="token string">'incrementAsync'</span><span class="token punctuation">,</span>
  amount<span class="token punctuation">:</span> <span class="token number">10</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>来看一个更加实际的购物车示例，涉及到<strong>调用异步 API</strong> 和<strong>分发多重 mutation</strong>：</p> <div class="language-js extra-class"><pre class="language-js"><code>actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token function">checkout</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> commit<span class="token punctuation">,</span> state <span class="token punctuation">}</span><span class="token punctuation">,</span> products<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 把当前购物车的物品备份起来</span>
    <span class="token keyword">const</span> savedCartItems <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token operator">...</span>state<span class="token punctuation">.</span>cart<span class="token punctuation">.</span>added<span class="token punctuation">]</span>
    <span class="token comment">// 发出结账请求，然后乐观地清空购物车</span>
    <span class="token function">commit</span><span class="token punctuation">(</span>types<span class="token punctuation">.</span><span class="token constant">CHECKOUT_REQUEST</span><span class="token punctuation">)</span>
    <span class="token comment">// 购物 API 接受一个成功回调和一个失败回调</span>
    shop<span class="token punctuation">.</span><span class="token function">buyProducts</span><span class="token punctuation">(</span>
      products<span class="token punctuation">,</span>
      <span class="token comment">// 成功操作</span>
      <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">commit</span><span class="token punctuation">(</span>types<span class="token punctuation">.</span><span class="token constant">CHECKOUT_SUCCESS</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token comment">// 失败操作</span>
      <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">commit</span><span class="token punctuation">(</span>types<span class="token punctuation">.</span><span class="token constant">CHECKOUT_FAILURE</span><span class="token punctuation">,</span> savedCartItems<span class="token punctuation">)</span>
    <span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>注意我们正在进行一系列的异步操作，并且通过提交 mutation 来记录 action 产生的副作用（即状态变更）。</p> <h3 id="在组件中分发-action"><a href="#在组件中分发-action" aria-hidden="true" class="header-anchor">#</a> 在组件中分发 Action</h3> <p>你在组件中使用 <code>this.$store.dispatch('xxx')</code> 分发 action，或者使用 <code>mapActions</code> 辅助函数将组件的 methods 映射为 <code>store.dispatch</code> 调用（需要先在根节点注入 <code>store</code>）：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> mapActions <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vuex'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
      <span class="token string">'increment'</span><span class="token punctuation">,</span> <span class="token comment">// 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`</span>

      <span class="token comment">// `mapActions` 也支持载荷：</span>
      <span class="token string">'incrementBy'</span> <span class="token comment">// 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      add<span class="token punctuation">:</span> <span class="token string">'increment'</span> <span class="token comment">// 将 `this.add()` 映射为 `this.$store.dispatch('increment')`</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="组合-action"><a href="#组合-action" aria-hidden="true" class="header-anchor">#</a> 组合 Action</h3> <p>Action 通常是异步的，那么如何知道 action 什么时候结束呢？更重要的是，我们如何才能组合多个 action，以处理更加复杂的异步流程？</p> <p>首先，你需要明白 <code>store.dispatch</code> 可以处理被触发的 action 的处理函数返回的 Promise，并且 <code>store.dispatch</code> 仍旧返回 Promise：</p> <div class="language-js extra-class"><pre class="language-js"><code>actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token function">actionA</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span>resolve<span class="token punctuation">,</span> reject<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'someMutation'</span><span class="token punctuation">)</span>
        <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>现在你可以：</p> <div class="language-js extra-class"><pre class="language-js"><code>store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'actionA'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>在另外一个 action 中也可以：</p> <div class="language-js extra-class"><pre class="language-js"><code>actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  <span class="token function">actionB</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> dispatch<span class="token punctuation">,</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'actionA'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'someOtherMutation'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>最后，如果我们利用 <a href="https://tc39.github.io/ecmascript-asyncawait/" target="_top" rel="noopener noreferrer">async / await<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，我们可以如下组合 action：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 假设 getData() 和 getOtherData() 返回的是 Promise</span>

actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token keyword">async</span> <span class="token function">actionA</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'gotData'</span><span class="token punctuation">,</span> <span class="token keyword">await</span> <span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token keyword">async</span> <span class="token function">actionB</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> dispatch<span class="token punctuation">,</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">await</span> <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'actionA'</span><span class="token punctuation">)</span> <span class="token comment">// 等待 actionA 完成</span>
    <span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'gotOtherData'</span><span class="token punctuation">,</span> <span class="token keyword">await</span> <span class="token function">getOtherData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><blockquote><p>一个 <code>store.dispatch</code> 在不同模块中可以触发多个 action 函数。在这种情况下，只有当所有触发函数完成后，返回的 Promise 才会执行。</p></blockquote></div> <div class="page-edit"><!----> <!----></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a indepth="true" href="mutations.html" class="prev">
          Mutation
        </a></span> <span class="next"><a indepth="true" href="modules.html">
          Module
        </a>
        →
      </span></p></div> <div class="bsa-cpc-wrapper"><div class="bsa-cpc"></div></div></div> <!----></div></div>
    
  




</body></html>
